import pygame


class Tetris():
    def __init__(self):
        # 设置游戏初始化
        pygame.init()
        # 设置最小尺寸
        self.size = 30
        # 设置游戏区域宽度数量
        self.width = 10
        # 设置高度数量
        self.height = 20
        # 是否循环
        self.is_loop = True
        # 设置背景颜色
        self.bg_color = (189, 183, 107)
        # 设置网格线颜色
        self.line_color = (154, 205, 50)
        # 设置字体和字号
        self.font1 = pygame.font.SysFont('Simhei', 18)
        # 设置字体颜色
        self.font_color = (255, 106, 106)
        self.level = 1

    def start(self):

        # 设置窗口大小
        self.screen = pygame.display.set_mode((self.size*(self.width+5), self.size*self.height))
        # 设置标题
        pygame.display.set_caption('俄罗斯方块')
        while self.is_loop:
            self.screen.fill(self.bg_color)
            self.event_handler()
            self.draw_grid()
            self.draw_text()
            # 更新屏幕
            pygame.display.update()
        self.quit()

    def event_handler(self):
        """事件处理助手"""
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.is_loop = False

    def draw_grid(self):
        """绘制游戏区域网格线"""
        for i in range(self.height+1):
            # 绘制线段
            pygame.draw.line(self.screen, self.line_color, (1, i*self.size), (self.width*self.size, i*self.size),2)
        for i in range(self.width+1):
            # 绘制线段
            pygame.draw.line(self.screen, self.line_color, (i*self.size, 0), (i*self.size, self.height*self.size), 2)

    def draw_text(self):
        level_img = self.font1.render(f'关卡: {self.level}', True , self.font_color )
        self.screen.blit(level_img, (self.size*(self.width+1), self.size*1))


    def quit(self):
        """退出游戏"""
        pygame.display.quit()





t = Tetris()
t.start()
